package cn.sytton.taffe.framework.security.handler;

import cn.sytton.taffe.framework.constants.ExceptionMessage;
import cn.sytton.taffe.framework.response.ResponseResult;
import cn.sytton.taffe.framework.util.JsonUtil;
import cn.sytton.taffe.framework.util.ResponseWriter;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

/**
 * 认证入口点
 * 认证异常时,被ExceptionTranslationFilter用于开启特定认证方案,处理response返回响应
 *
 * @author skyrock
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) {
        ResponseWriter.print(response, 401, JsonUtil.getString(ResponseResult.fail(ExceptionMessage.UNAUTHORIZED)));
    }
}
